package com.keai.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.keai.constant.MessageConst;
import com.keai.entity.PageResult;
import com.keai.entity.QueryPageBean;
import com.keai.entity.Result;
import com.keai.pojo.Permission;
import com.keai.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: SpringMVC_test
 * @author: Mr.Dong
 * @create: 2019-08-24 16:33
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    PermissionService permissionService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=  permissionService.findPage(queryPageBean);
        return pageResult;

    }
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true, MessageConst.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_PERMISSION_FALL);
        }
    }
    @RequestMapping("/delById")
    public Result delById(Integer id){
        try {
            permissionService.delById(id);
            return new Result(true,MessageConst.DELETE_PERMISSION_SUCCESS);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_PERMISSION_FALL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Permission permission=  permissionService.findById(id);
            return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_PERMISSION_FALL);
        }
    }


    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true,MessageConst.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_PERMISSION_FALL);
        }
    }

    @RequestMapping("/findAll")
    public  Result findAll(){
        List<Permission> permissionList= permissionService.findAll();
        return new Result(true,MessageConst.ADD_PERMISSION_SUCCESS,permissionList);
    }

}
